<?php
/**
 * Moduł zarządzania grupami
 * @author Miron Pawlik
 */


if($user->isLocked())
{
	header("Location: ".$_conf['host']);
	exit;
}
//dodawanie grupy
if($fd['mode'] == 'add' && is_numeric($fd['subject']))
{
	if($db->CountRec('groups', 'subjectID = '.$fd['subject'].' AND ownerID = '.$user->getId()))
	{
		$errorMsg = "Posiadasz już grupę w tym przedmiocie";
	}
	else
	{
		$db->deleteAll('groups', 'subjectID = '.$fd['subject'].' AND memberID = '.$user->getId());
		$db->addNew('groups', array('subjectID' => $fd['subject'], 'ownerID' => $user->getId(), 'memberID' => $user->getId()));
		header("Location: ".$_conf['host'].'?groups');
	}
}
if($fd['mode'] == 'join')
{
	
	$groupsCapacity = $db->getData('subjects', '', 'subjectID', 'groups');
	
	foreach($fd['subject'] as $subID => $ownID)
	{
		if(!is_numeric($subID) || !is_numeric($ownID))
			continue;
		
		if(!$db->getById($subID, 'subjects', 'subjectID', 'subjectID'))
			continue;
		
		if($db->CountRec('groups', 'ownerID = '.$user->getId().' AND subjectID = '.$subID) > 0)
			continue;
		
		$number = $db->CountRec('groups', 'ownerID = '.$ownID.' AND subjectID = '.$subID);
		
			var_dump($db->getAll('groups'));
		if($ownID == 0)
			$db->deleteAll('groups', 'subjectID = '.$subID.' AND memberID = '.$user->getId());
		if($number > 0 && $number < $groupsCapacity[$subID])
		{
		var_dump($ownId);
			$db->deleteAll('groups', 'subjectID = '.$subID.' AND memberID = '.$user->getId());
			$db->addNew('groups', array('ownerID'=>$ownID, 'subjectID'=>$subID, 'memberID'=>$user->getId()));
		}
	}
}
//uwuwanie...
if($fd['mode'] == 'del')
{
	//... grupy, wraz ze wszystkimi czlonkami
	if(is_numeric($fd['group']))
	{
		
		$db->deleteAll('groups', 'subjectID = '.$fd['group'].' AND ownerID = '.$user->getId());
		header("Location: ".$_conf['host'].'?groups');
	}
	//...czlonka z grupy
	//@TODO: brak kastracji (usuwania czlonka)
}
$query = $db->query("SELECT * FROM subjects AS s JOIN studentSubjects AS ss ON ss.subjectID = s.subjectID WHERE s.groups > 1 AND ss.indexID = ".$user->getId());
$subjects = $query->fetchAll();
$query->closeCursor();
$smarty->assign('subjects', $subjects);

//pobieranie listy grup
$myGroups = $db->getData('groups', 'memberID = '.$user->getId(), 'subjectID', 'ownerID');
$listOfGroups = array();


foreach($subjects as $sub)
{
	$query = $db->query('SELECT * FROM groups AS g JOIN students AS s ON s.indexID = g.ownerID WHERE g.ownerID = g.memberID AND g.subjectID = '.$sub['subjectID'].' ORDER BY s.surname, s.name');
	$listOfGroups[$sub['subjectID']] = $query->fetchAll();
	$query->closeCursor();
	
	
}

//pobieranie wlasnych grup
$query = $db->query("SELECT * FROM groups AS g JOIN subjects AS s on s.subjectID = g.subjectID WHERE g.ownerID = g.memberID AND g.ownerID = ".$user->getId());
$ownGroups = $query->fetchAll();
$query->closeCursor();

//pobieranie czlonkow wlasnych grup
$ownGroupsMembers = array();
foreach($ownGroups as $ow)
{
	$query = $db->query('SELECT * FROM groups AS g JOIN students AS s ON s.indexID = g.memberID WHERE g.subjectID = '.$ow['subjectID'].' AND g.ownerID = '.$user->getId().' ORDER BY s.surname, s.name');
	$ownGroupsMembers[$ow['subjectID']] = $query->fetchAll();
	$query->closeCursor(); 
}


$smarty->assign('listOfGroups', $listOfGroups);
$smarty->assign('myGroups', $myGroups);
$smarty->assign('ownGroups', $ownGroups);
$smarty->assign('ownGroupsMembers', $ownGroupsMembers);
$smarty->assign('errorMsg', $errorMsg);
?>